#!/bin/bash

# Lazy "checkboxes" in bash script
# (for those who dont want to use whiptail)
#
# My most complex (and cryptic... and useless) script so far
# answers http://serverfault.com/questions/144939
#
#
options=("AAA" "BBB" "CCC" "DDD")

menu() {
	echo 'Avaliable options:'
	for i in ${!options[@]}; do 
		printf '%3d%s) %s\n' $((i+1)) "${choices[i]:- }" "${options[i]}"
	done
	[[ "$msg" ]] && echo "$msg"; :
}

prompt='Check an option (again to uncheck, ENTER when done): '
while menu && read -rp "$prompt" num && [[ "$num" ]]; do
	[[ "$num" != *[![:digit:]]* ]] && num=$((10#$num)) && /
	(( num > 0 && num <= ${#options[@]} )) || {
		msg="Invalid option: $num"; continue
	}
	((num--)); msg="${options[num]} was ${choices[num]:+un}checked"
	[[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+"
done

printf 'You selected'; msg=' nothing'
for i in ${!options[@]}; do 
	[[ "${choices[i]}" ]] && { printf ' %s' "${options[i]}"; msg=; }
done
echo "$msg"
